//index.js
const app = getApp()
const db = wx.cloud.database()
const _ = db.command
Page({
  data: {
    avatarUrl: './user-unlogin.png',
    inputValue: '',
    listTip: [],
    checkedArr: [],
    unCheckedArr: [],
    show:false
  },

  onLoad: function() {
    this.getTip()
  },
  changeInput(e) {
    this.setData({
      inputValue: e.detail.value
    })
  },
  addTip() {
    let tip = this.data.inputValue
    if (tip === '') {
      wx.showToast({
        title: '请输入内容',
        icon: 'none',
        duration: 2000
      })
      return
    }
    db.collection('todos').add({
      data: {
        date: new Date(),
        tip: tip,
        checked: false
      }
    }).then(res => {
      console.log(res)
    }).catch(err => {
      console.error(err)
    })
    this.setData({
      inputValue: ''
    })
    this.getTip()
    wx.showToast({
      title: '添加成功',
      icon: 'success',
      duration: 2000
    })

  },
  getTip() {
    wx.cloud.callFunction({
      name: 'getAllTip',
    }).then(data => {
      console.log(data)
      // 获得数据
      const {
        data: res
      } = data.result
      this.setData({
        listTip: res
      })
    })
  },
  //取数组不同部分
  getArrDifference(arr1, arr2) {
    return arr1.concat(arr2).filter(function(v, i, arr) {
      return arr.indexOf(v) === arr.lastIndexOf(v);
    });

  },
  // 修改页面的勾选状态及处理数据
  updateTip(e) {
    console.log(e.detail.value)
    let length = this.data.listTip.length
    let arr = this.data.listTip
    let checkedArr = e.detail.value
    var allCheck = []
    this.data.listTip.forEach(item => {
      allCheck.push(item._id)
    })
    let unCheckedArr = this.getArrDifference(allCheck, checkedArr)
    arr.forEach(item=>{
      item.checked = false
    })
    checkedArr.forEach(id=>{
      for (let i = 0; i < length;i++){
        if(arr[i]._id===id){
          arr[i].checked = true
        }
      }
    })
    this.setData({
      checkedArr,
      unCheckedArr,
      listTip:arr
    })
  },
  editBtn(e){
    e.currentTarget.dataset.item.show= true
    this.setData({
      show:true
    })
    console.log(e)
  },
  editInput(){
    this.setData({
      show: false
    })
  },
  // 点击保存按钮触发更新数据库操作
  updateBtn() {

    this.data.checkedArr.forEach(id => {
      db.collection('todos').doc(id).update({
        data: {
          checked: true
        }
      }).then(wx.showToast({
        title: '保存成功',
        icon: 'success',
        duration: 2000
      }))
        .catch(console.error)
    }),
      this.data.unCheckedArr.forEach(id => {
        db.collection('todos').doc(id).update({
          data: {
            checked: false
          }
        }).then(wx.showToast({
          title: '保存成功',
          icon: 'success',
          duration: 2000
        }))
          .catch(console.error)
      })
  },
  // 点击删除按钮触发删除数据库数据操作
  delBtn(e) {
    let id = e.currentTarget.dataset.id
    db.collection('todos').doc(id).remove()
      .then(console.log)
      .catch(console.error)
    let arr = this.data.listTip;
    for (var j = 0; j < arr.length; j++) {
      if (arr[j]._id == id) {
        arr.splice(j, 1);
        console.log(arr)
        break;
      }
    }
    this.setData({
      listTip: this.data.listTip,
    })
    wx.showToast({
      title: '删除成功',
      icon: 'success',
      duration: 2000
    })
  }

})